package cn.usian.service.employeeserviceimpl;

import cn.usian.common.R;
import cn.usian.dao.DishFlavorMapper;
import cn.usian.dao.DishMapper;
import cn.usian.entity.Category;
import cn.usian.entity.Dish;
import cn.usian.entity.DishDTO;
import cn.usian.entity.DishFlavor;
import cn.usian.exception.NameException;
import cn.usian.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;



    @Override
    public void updateone(Long[] ids,Integer status) {
        for (Long id : ids) {
            dishMapper.updateone(id,status);
        }
    }

    @Override
    public R findPage(Page<DishDTO> page1, String name) {
        List<DishDTO>list=dishMapper.findPage(page1,name);
        page1.setRecords(list);
        return R.success(page1);
    }

    @Override
    public R delDish(Long[] ids) {
        for (Long id : ids) {
            dishMapper.updateByIds(id);
        }
        return R.success("删除成功");
    }

    @Transactional
    @Override
    public R addDish(DishDTO dishDTO) {
        dishDTO.setStatus(1);
        dishDTO.setSort(0);
        dishDTO.setCreateTime(LocalDateTime.now());
        dishDTO.setUpdateTime(LocalDateTime.now());
        dishMapper.insert(dishDTO);
        List<DishFlavor> flavorList = dishDTO.getList();
        for (DishFlavor dishFlavor : flavorList) {
            dishFlavor.setDishId(dishDTO.getId());
            dishFlavor.setCreateTime(dishDTO.getCreateTime());
            dishFlavor.setUpdateTime(dishDTO.getUpdateTime());
            dishFlavor.setCreateUser(dishDTO.getCreateUser());
            dishFlavor.setUpdateUser(dishDTO.getUpdateUser());
            dishFlavor.setIsDeleted(0);
        }
        for (DishFlavor dishFlavor : flavorList) {
            dishFlavorMapper.insert(dishFlavor);
        }
        return R.success("添加成功");
    }

    public Dish findByName(Dish dish) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        if(dish.getName()!=null && !dish.getName().equals("")){
            queryWrapper.eq(Dish::getName,dish.getName());
        }
        return dishMapper.selectOne(queryWrapper);
    }

    @Override
    public R list(Dish dish) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = dishMapper.selectList(queryWrapper);
        return R.success(dishList);
    }

    @Override
    public R findById(Long id) {
        Dish dish=dishMapper.selectById(id);
        List<DishFlavor>list=dishFlavorMapper.findByDishId(id);
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish,dishDTO);
        dishDTO.setList(list);
        return R.success(dishDTO);
    }



    @Override
    public R updateDish(DishDTO dishDTO) {
        dishDTO.setUpdateTime(LocalDateTime.now());
        dishMapper.updateById(dishDTO);
        //先删除口味信息
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //再添加
        List<DishFlavor> flavorList = dishDTO.getList();
        for (DishFlavor dishFlavor : flavorList) {
            dishFlavorMapper.insert(dishFlavor);
        }
        return R.success(dishDTO);
    }




}
